//Ztree生成
var ZtreeObj;
function addLayer(url, id){
    var title= "添加信息";
    url = getContextPath() + url ;
    if(id){//编辑
        title = "修改信息";
        //以下判断是以防想带其他参数都带不了
        if(url.indexOf("?")>-1){
            url += '&id=' + id;
        }else{
            url += '?id=' + id;
        }
    }
    layer.open({
        title : title,
        type: 2,
        scrollbar :true,
        area: ['90%', '90%'],
        anim: 2,
        content: [url, 'yes']
    });
}

function modi(url){
    var checkStatus = table.checkStatus('tableList')
        ,data = checkStatus.data;
    if(data.length == 0 || data.length > 1){
        layer.msg("请选择一条数据");
        return false;
    }
    addLayer(url, data[0].id);
}

function saveOrUpdate(id, fristHalfUrl, data) {
    var url = fristHalfUrl + "/add/save";
    if(id){//编辑
        url = fristHalfUrl + "/edit/save";
    }
    $ajax({
        url: url,
        data :  data.field,
        succCallback: function (json) {
            if(json.code == 0){
                if(id){  //修改树节点
                    var node = parent.ZtreeObj.getNodeByParam("id", id, null);
                    node.name= data.field.name;
                    parent.ZtreeObj.updateNode(node);
                }else{  //添加树节点
                    var parentId = $("#parentId").val();
                    var parentNode = parent.ZtreeObj.getNodeByParam("id", parentId, null);
                    var newNode = {id: json.data.id, name: json.data.name,dropInner:false,childOuter:false};
                    parent.ZtreeObj.addNodes(parentNode, -1, newNode ,true);
                }
                parent.reload();
                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);//关闭弹出的子页面窗口
                parent.layer.msg(json.msg);
            }
        }
    });
    return false;//阻止表单跳转。如果需要表单跳转，去掉这段即可。
}

function deleteById(data,obj,url){
    layer.confirm('真的删除此项记录么', function(index){
        $ajax({
            url: url,
            data: {id : data.id},
            succCallback: function (json) {
                if(json.code == 0){
                    obj.del();
                    var deleteNode = ZtreeObj.getNodeByParam("id", data.id, null);
                    ZtreeObj.removeNode(deleteNode);
                }
            }
        });
    });
}

function deleteBatch(url){
    var data = table.checkStatus('tableList').data;
    var ids = "";
    if(data.length == 0 ){
        layer.msg("请至少选择一条数据");
        return false;
    }
    for (var i = 0; i < data.length; i++) {
        ids += data[i].id;
        if(i != (data.length -1))
            ids += ",";
    }
    layer.confirm('您确定要批量删除数据吗？此操作不可逆转，请谨慎使用！', function(index){
        $ajax({
            url: url,
            data: {"ids" : ids},
            succCallback: function (json) {
                if(json.code == 0){
                    for (var n = 0; n < data.length; n++) {
                        var deleteNode = ZtreeObj.getNodeByParam("id", data[n].id, null);
                        ZtreeObj.removeNode(deleteNode);
                    }
                    reload();
                }
            }
        });
    });
}

function treeReload(url,queryTop,isFirst) {
    $.ajax({
        url: getContextPath() + url,
        data : {
            "queryTop":queryTop,
            "isFirst" : isFirst
        },
        type: "post",
        dataType: "text",
        success: function (data) {
            var zNodes = eval("(" + data + ")");
            ZtreeObj = $.fn.zTree.init($("#treeRegion"), setting, zNodes);
        },
        error: function (XMLHttpRequest, textStatus) {
            layer.msg(textStatus);
        }
    });
}